package cn.woblog.recyclerviewsample.view;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.os.Build.VERSION_CODES;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import cn.woblog.recyclerviewsample.util.ScreenUtil;

/**
 * Created by renpingqing on 4/12/17.
 */

public class SwipeMenuLayout extends HorizontalScrollView {

  private int screenWidth;
  private int menuMeasuredWidth;
  private int touchSlop;
  private OnSwipeListener onSwipeListener;
  private boolean isSwiped = false;

  public SwipeMenuLayout(Context context) {
    super(context);
    init();
  }

  public SwipeMenuLayout(Context context,
      @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public SwipeMenuLayout(Context context,
      @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  @RequiresApi(api = VERSION_CODES.LOLLIPOP)
  public SwipeMenuLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
  }

  private void init() {
    ViewConfiguration configuration = ViewConfiguration.get(getContext());
    // 获取TouchSlop值
    touchSlop = configuration.getScaledTouchSlop();
    screenWidth = ScreenUtil.getScreenWidth(getContext());
    setVerticalScrollBarEnabled(false);
    setHorizontalScrollBarEnabled(false);

  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    ViewGroup viewGroup = (ViewGroup) getChildAt(0);
    View contentView = viewGroup.getChildAt(0);
    View menuView = viewGroup.getChildAt(1);
    menuMeasuredWidth = menuView.getMeasuredWidth();
    contentView.setLayoutParams(
        new LinearLayout.LayoutParams(screenWidth, LinearLayout.LayoutParams.WRAP_CONTENT));
//    menuView.setLayoutParams(new LinearLayout.LayoutParams(screenWidth/2, LinearLayout.LayoutParams.WRAP_CONTENT));
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    return super.onInterceptTouchEvent(ev);
  }

  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
      case MotionEvent.ACTION_DOWN:
        if (onSwipeListener != null) {
          onSwipeListener.onStartSwipe(this);
        }
        break;
      case MotionEvent.ACTION_UP:
        Log.d("TAG", "up:" + getScrollX());
        if (getScrollX() > menuMeasuredWidth / 2) {
          //显示菜单
//          scrollTo(menuMeasuredWidth, 0);
//          scroller.startScroll(getScrollX(),0,menuMeasuredWidth,0);
          startScrollToX(getScrollX(), menuMeasuredWidth);
          isSwiped = true;
          if (onSwipeListener != null) {
            onSwipeListener.onSwiped(this);
          }
        } else {
          //隐藏菜单
//          scrollTo(0, 0);
//          scroller.startScroll(getScrollX(),0,0,0);
          closeMenu();
          if (onSwipeListener != null) {
            onSwipeListener.onClose(this);
          }
        }
        invalidate();
        break;
    }
    super.onTouchEvent(ev);
    return true;
  }

  private void closeMenu() {
    startScrollToX(getScrollX(), 0);
    isSwiped = false;
  }

  private void startScrollToX(int scrollX, int scrollXt) {
    ValueAnimator valueAnimator = ValueAnimator.ofInt(scrollX, scrollXt);
    valueAnimator.setDuration(300);
    valueAnimator.setInterpolator(new DecelerateInterpolator());
    valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        scrollTo((Integer) animation.getAnimatedValue(), 0);
      }
    });
    valueAnimator.start();
  }

  public boolean isSwiped() {
    return isSwiped;
  }


  public void close() {
    closeMenu();
    invalidate();
  }

  public void setOnSwipeListener(
      OnSwipeListener onSwipeListener) {
    this.onSwipeListener = onSwipeListener;
  }

  public interface OnSwipeListener {

    void onStartSwipe(SwipeMenuLayout swipeMenuLayout);

    void onSwiped(SwipeMenuLayout swipeMenuLayout);

    void onClose(SwipeMenuLayout swipeMenuLayout);
  }
}
